import { evaluate, parse } from './formula';

//公式解析
function evalFormual(expression: string, data: any) {
  return evaluate(expression, data, {
    evalMode: true,
  });
}
//公式校验
function validate(value: string) {
  try {
    value &&
      parse(value, {
        evalMode: true,
        allowFilter: false,
      });
    return true;
  } catch (e: any) {
    if (/\s(\d+:\d+)$/.test(e.message)) {
      const [, position] = /\s(\d+:\d+)$/.exec(e.message) || [];
      return '公式值校验错误，错误的位置/原因是 ' + position;
    }
    return e.message;
  }
}

export { evalFormual, validate };
